package cc.rengu.igas.route.biz.service.impl;

import cc.rengu.igas.route.common.enums.RouteTxnNumEnum;
import cc.rengu.igas.route.common.enums.SmartRouteRspEnums;
import cc.rengu.igas.route.facade.SmartRouteService;
import cc.rengu.igas.route.facade.request.*;
import cc.rengu.igas.route.facade.response.*;
import cc.rengu.igas.route.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 智能路由实现类
 *
 * @author xionglz
 * @version 1.0
 * @date 2020-03-12
 */
@SuppressWarnings({"unchecked"})
public class SmartRouteServiceImpl implements SmartRouteService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<SmartRouteResponse> smartRouteChoice(SmartRouteRequest smartRouteRequest) {
        DubboService<SmartRouteRequest> dubboService = new DubboServiceImpl();
        Result<SmartRouteResponse> result = new Result<>();
        try {
            DubboResult<SmartRouteResponse> dubboResult = dubboService.callAtomService(smartRouteRequest,
                    RouteTxnNumEnum.ROUTER_QUERY.getService(), SmartRouteResponse.class);
            result.setResult(dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            rglog.error("路由查询失败:<{}>", StringUtil.ExceptionToString(e));
            SmartRouteResponse smartRouteResponse = new SmartRouteResponse();
            smartRouteResponse.setHeader(smartRouteRequest.getHeader());
            result.setResult(smartRouteResponse);
            result.setSuccess(false);
            if (e instanceof BizException) {
                smartRouteResponse.setRespDesc(((BizException) e).getCode());
                smartRouteResponse.setRespDesc(e.getMessage());
            } else {
                smartRouteResponse.setRespCode(SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                smartRouteResponse.setRespDesc(SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<IntelligenceCreditResponse> externalPayment(IntelligenceCreditRequest intelligenceCreditRequest) {
        DubboService<IntelligenceCreditRequest> dubboService = new DubboServiceImpl();
        Result<IntelligenceCreditResponse> result = new Result<>();
        try {
            DubboResult<IntelligenceCreditResponse> dubboResult = dubboService.callAtomService(
                    intelligenceCreditRequest, RouteTxnNumEnum.CREDIT.getService(), IntelligenceCreditResponse.class);
            result.setResult(dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            rglog.error("对外付款失败:<{}>", StringUtil.ExceptionToString(e));
            IntelligenceCreditResponse intelligenceCreditResponse = new IntelligenceCreditResponse();
            intelligenceCreditResponse.setHeader(intelligenceCreditRequest.getHeader());
            result.setResult(intelligenceCreditResponse);
            result.setSuccess(false);
            if (e instanceof BizException) {
                intelligenceCreditResponse.setRespDesc(((BizException) e).getCode());
                intelligenceCreditResponse.setRespDesc(e.getMessage());
            } else {
                intelligenceCreditResponse.setRespCode(SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                intelligenceCreditResponse.setRespDesc(SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<IntelligencePaymentSignResponse> paymentSign(IntelligencePaymentSignRequest intelligencePaymentSignRequest) {
        DubboService<IntelligencePaymentSignRequest> dubboService = new DubboServiceImpl();
        Result<IntelligencePaymentSignResponse> result = new Result<>();
        try {
            DubboResult<IntelligencePaymentSignResponse> dubboResult = dubboService.callAtomService(
                    intelligencePaymentSignRequest, RouteTxnNumEnum.PAYMENT_SIGN.getService(), IntelligencePaymentSignResponse.class);
            result.setResult(dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            rglog.error("对外收款签约失败:<{}>", StringUtil.ExceptionToString(e));
            IntelligencePaymentSignResponse intelligencePaymentSignResponse = new IntelligencePaymentSignResponse();
            intelligencePaymentSignResponse.setHeader(intelligencePaymentSignRequest.getHeader());
            result.setResult(intelligencePaymentSignResponse);
            result.setSuccess(false);
            if (e instanceof BizException) {
                intelligencePaymentSignResponse.setRespDesc(((BizException) e).getCode());
                intelligencePaymentSignResponse.setRespDesc(e.getMessage());
            } else {
                intelligencePaymentSignResponse.setRespCode(SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                intelligencePaymentSignResponse.setRespDesc(SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<IntelligencePaymentSignVerifyResponse> paymentSignVerify(
            IntelligencePaymentSignVerifyRequest intelligencePaymentSignVerifyRequest) {
        DubboService<IntelligencePaymentSignVerifyRequest> dubboService = new DubboServiceImpl();
        Result<IntelligencePaymentSignVerifyResponse> result = new Result<>();
        try {
            DubboResult<IntelligencePaymentSignVerifyResponse> dubboResult = dubboService.callAtomService(
                    intelligencePaymentSignVerifyRequest, RouteTxnNumEnum.PAYMENT_SIGN_VERIFY.getService(),
                    IntelligencePaymentSignVerifyResponse.class);
            result.setResult(dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            rglog.error("对外收款签约确认失败:<{}>", StringUtil.ExceptionToString(e));
            IntelligencePaymentSignVerifyResponse intelligencePaymentSignVerifyResponse = new IntelligencePaymentSignVerifyResponse();
            intelligencePaymentSignVerifyResponse.setHeader(intelligencePaymentSignVerifyRequest.getHeader());
            result.setResult(intelligencePaymentSignVerifyResponse);
            result.setSuccess(false);
            if (e instanceof BizException) {
                intelligencePaymentSignVerifyResponse.setRespDesc(((BizException) e).getCode());
                intelligencePaymentSignVerifyResponse.setRespDesc(e.getMessage());
            } else {
                intelligencePaymentSignVerifyResponse.setRespCode(SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                intelligencePaymentSignVerifyResponse.setRespDesc(SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 对外收款
     *
     * @param intelligencePaymentRequest 对外收款请求即支付、代收等请求
     * @return Result<IntelligencePaymentResponse>
     * @author lizhangtao
     * @date 2020-04-29
     **/
    @Override
    public Result<IntelligencePaymentResponse> payment(IntelligencePaymentRequest intelligencePaymentRequest) {
        DubboService<IntelligencePaymentRequest> dubboService = new DubboServiceImpl();
        Result<IntelligencePaymentResponse> result = new Result<>();
        try {
            DubboResult<IntelligencePaymentResponse> dubboResult = dubboService.callAtomService(
                    intelligencePaymentRequest, RouteTxnNumEnum.PAYMENT.getService(), IntelligencePaymentResponse.class);
            result.setResult(dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            rglog.error("对外收款失败:<{}>", StringUtil.ExceptionToString(e));
            IntelligencePaymentResponse intelligencePaymentResponse = new IntelligencePaymentResponse();
            intelligencePaymentResponse.setHeader(intelligencePaymentRequest.getHeader());
            result.setResult(intelligencePaymentResponse);
            result.setSuccess(false);
            if (e instanceof BizException) {
                intelligencePaymentResponse.setRespDesc(((BizException) e).getCode());
                intelligencePaymentResponse.setRespDesc(e.getMessage());
            } else {
                intelligencePaymentResponse.setRespCode(SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                intelligencePaymentResponse.setRespDesc(SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 对外收款确认
     *
     * @param intelligencePaymentVerifyRequest 对外收款请求即支付、代收等请求的确认
     * @return Result<IntelligencePaymentVerifyResponse>
     * @author lizhangtao
     * @date 2020-04-29
     **/
    @Override
    public Result<IntelligencePaymentVerifyResponse> paymentVerify(IntelligencePaymentVerifyRequest intelligencePaymentVerifyRequest) {
        DubboService<IntelligencePaymentVerifyRequest> dubboService = new DubboServiceImpl();
        Result<IntelligencePaymentVerifyResponse> result = new Result<>();
        try {
            DubboResult<IntelligencePaymentVerifyResponse> dubboResult = dubboService.callAtomService(
                    intelligencePaymentVerifyRequest, RouteTxnNumEnum.PAYMENT_VERIFY.getService(), IntelligencePaymentResponse.class);
            result.setResult(dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            rglog.error("对外收款确认:<{}>", StringUtil.ExceptionToString(e));
            IntelligencePaymentVerifyResponse intelligencePaymentVerifyResponse = new IntelligencePaymentVerifyResponse();
            intelligencePaymentVerifyResponse.setHeader(intelligencePaymentVerifyRequest.getHeader());
            result.setResult(intelligencePaymentVerifyResponse);
            result.setSuccess(false);
            if (e instanceof BizException) {
                intelligencePaymentVerifyResponse.setRespDesc(((BizException) e).getCode());
                intelligencePaymentVerifyResponse.setRespDesc(e.getMessage());
            } else {
                intelligencePaymentVerifyResponse.setRespCode(SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                intelligencePaymentVerifyResponse.setRespDesc(SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<IntelligenceResultQueryResponse> resultQuery(IntelligenceResultQueryRequest intelligenceResultQueryRequest) {
        DubboService<IntelligenceResultQueryRequest> dubboService = new DubboServiceImpl();
        Result<IntelligenceResultQueryResponse> result = new Result<>();
        try {
            DubboResult<IntelligenceResultQueryResponse> dubboResult = dubboService.callAtomService(
                    intelligenceResultQueryRequest, RouteTxnNumEnum.RESULT_QUERY.getService(), IntelligenceResultQueryResponse.class);
            result.setResult(dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            rglog.error("对外收款确认:<{}>", StringUtil.ExceptionToString(e));
            IntelligenceResultQueryResponse queryResponse = new IntelligenceResultQueryResponse();
            queryResponse.setHeader(intelligenceResultQueryRequest.getHeader());
            result.setResult(queryResponse);
            result.setSuccess(false);
            if (e instanceof BizException) {
                queryResponse.setRespDesc(((BizException) e).getCode());
                queryResponse.setRespDesc(e.getMessage());
            } else {
                queryResponse.setRespCode(SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                queryResponse.setRespDesc(SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }
}
